package cn.hpclub.server.controller.admin;

import cn.hpclub.server.bean.SystemConfig;
import cn.hpclub.server.model.CmccOrderVmember;
import cn.hpclub.server.service.CmccOrderGcgmService;
import cn.hpclub.server.service.CmccOrderVmemberService;
import cn.hpclub.server.util.ExcelLargeDataUtils;
import cn.hpclub.server.util.ExcelRenderUtil;
import cn.hpclub.server.util.InterfaceUtil;
import cn.hpclub.server.util.SystemConfigUtil;
import com.alibaba.fastjson.JSONObject;
import com.jfinal.core.JFinal;
import com.jfinal.plugin.activerecord.Page;
import org.apache.commons.lang.StringUtils;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.concurrent.ConcurrentHashMap;

/**
 * Created by wanglin on 2017/4/5.
 */
public class VmemberOrderController extends BaseAdminController<CmccOrderVmember>{
    private static final String ORG_LIST = "orgList";
    private static final String ORG_ID   = "org_id";
    private static final String CREATE_DATE = "create_date";
    private static final String START_TIME  = "startTime";
    private static final String END_TIME    = "endTime";
    private static Logger logger   = LoggerFactory.getLogger(VnetReportController.class);

    public void list(){

        int defaultOrgId = initOrgTree();
        ConcurrentHashMap<String, String> searchInfo = getSearchInfo();
        if(searchInfo.get(ORG_ID) == null){
            searchInfo.put(ORG_ID, String.valueOf(defaultOrgId));
        }
        if(searchInfo.get(CREATE_DATE) == null){
            try{
                setDateParam(searchInfo);
            }
            catch(ParseException e){
                e.printStackTrace();
            }
        }
            query(searchInfo);
    }

    private void setDateParam(ConcurrentHashMap<String, String> searchInfo) throws ParseException{
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String endDate = sdf.format(new java.util.Date());
        Calendar c = Calendar.getInstance();
        c.setTime(sdf.parse(endDate));
        c.add(Calendar.MONTH, -1); // 得到前一个月
        String startDate = sdf.format(c.getTime());
        String createDate = startDate + "|" + endDate;

        searchInfo.put(START_TIME, startDate);
        searchInfo.put(END_TIME, endDate);
        searchInfo.put(CREATE_DATE, createDate);
    }

    /*
     * private int initOrgTree(){ List<Entry<String, String>> orgList =
     * InterfaceUtil.getOrgList(); setAttr(ORG_LIST, orgList); if(orgList.size()
     * > 0){ return Integer.parseInt(orgList.get(0).getValue()); } else{ return
     * 0; } }
     */

    private void query(ConcurrentHashMap<String, String> searchInfo){
        getPage(searchInfo);
        setAttrs(searchInfo);
        render("/admin/cmcc_order_vmember_list.tpl");
    }

    @SuppressWarnings("static-access")
    private void getPage(ConcurrentHashMap<String, String> searchInfo){
        String select = CmccOrderVmemberService.getSelectSql();

        String sqlExceptSelect = CmccOrderVmemberService.service.getSqlExceptSelect(searchInfo);
        int pageNumber = getParaToInt("pageNumber", 1);
        Page<CmccOrderVmember> pager = CmccOrderVmember.dao.paginate(pageNumber, getParaToInt("pageSize", 10), select,
                sqlExceptSelect);
        /* 删除最后一页所有数据后，显示最后一页前一页 */
        if(pager.getPageNumber() > 1 && pager.getList().size() == 0){
            pager = CmccOrderVmember.dao.paginate(pageNumber - 1, getParaToInt("pageSize", 10), select, sqlExceptSelect);
        }

        setAttr("pager", pager);
    }

    public void export(){
        ConcurrentHashMap<String, String> searchInfo = getSearchInfo();
        if(!searchInfo.containsKey("org_id") || "-1".equals(searchInfo.get("org_id"))){
            searchInfo.put("org_id", InterfaceUtil.getOrgId() + "");
        }
        SXSSFWorkbook workbook = new CmccOrderVmemberService().service.getExcel(searchInfo);
        render(new ExcelRenderUtil(workbook, "V网成员报表"));
    }

    String generateFileName(){
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss");
        String curTime = sdf.format(date);
        String fileName = "V网成员报表-" + curTime + ".xlsx";
        return fileName;
    }

}
